package com.lizardmind.haofudoctor.ui.diagnosis.visit.userFill

import android.app.Activity
import android.arch.lifecycle.ViewModelProviders
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.EncodeUtils
import com.blankj.utilcode.util.ImageUtils
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.util.getDataBinding
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.ActivityFillVisitBinding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.Request
import com.lizardmind.haofudoctor.other.VisitBean
import com.lizardmind.haofudoctor.ui.other.EvaluationActivity
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.include_title_text.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.startActivityForResult

/**
 * 用户填写复诊单
 * Created by Extends on 2017/11/17 15:07
 */
class FillVisitActivity:BaseActivity() {
    private val binding by lazy { getDataBinding<ActivityFillVisitBinding>(R.layout.activity_fill_visit) }
    override fun onCreatRootView(): View = binding.root
    private lateinit var id:String

    override fun onInitView(savedInstanceState: Bundle?) {
        id = intent.getStringExtra("id")
        //初始化标题栏
        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.onClick { finish() }
        titleText.text = "复诊单"

        binding.apply {
            vm = ViewModelProviders.of(this@FillVisitActivity).get(FillVisitVM::class.java)
            lifecycle.addObserver(vm!!)

            afvS0.onClick { clickTongue() }
            afvS1.onClick { clickTongue() }

            afvEvaluation.onClick {
                startActivityForResult<EvaluationActivity>(100,"bean" to vm!!.bean.get().doctor,"fatherType" to "1","fatherId" to vm!!.bean.get().id)
            }

            afvNext.onClick {
                //提交复诊单
                val tonguePicture = vm!!.tongue_picture.map { ImageUtils.bitmap2Bytes(ImageUtils.getBitmap(it.compressPath), Bitmap.CompressFormat.JPEG) }
                        .joinToString(","){ EncodeUtils.base64Encode2String(it)}
                observe {
                    postApi<Request<String>>(with(vm!!){
                        hashMapOf("action" to "fill_visit",
                                "token" to DoctorInfo.token,
                                "id" to id,
                                "feel" to select.get().toString(),
                                "improved" to improved.get(),
                                "improved_not" to improved_not.get(),
                                "new_symptoms" to new_symptoms.get(),
                                "tongue_picture" to tonguePicture)
                    }, Consumer {
                        it.dispose {
                            finish()
                        }
                    },context = this@FillVisitActivity)
                }

            }
        }

        observe {
            postApi<Request<VisitBean>>(hashMapOf(
                    "action" to "visit_info",
                    "token" to DoctorInfo.token,
                    "id" to id
            ), Consumer {
                it.dispose {
                    binding.vm!!.updateBean(it)
                }
            })
        }
    }

    fun clickTongue(){
        val temp = binding.vm!!.tongue_picture
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()
                .selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                .maxSelectNum(2)
                .previewImage(true)// 是否可预览图片 true or false
                .isCamera(true)// 是否显示拍照按钮 true or false
                .compress(true)// 是否压缩 true or false
                .selectionMedia(temp)
                .minimumCompressSize(100)
                .cropCompressQuality(60)
                .forResult(PictureConfig.CHOOSE_REQUEST)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(resultCode== Activity.RESULT_OK){
            if(requestCode== PictureConfig.CHOOSE_REQUEST){
                val selectList = PictureSelector.obtainMultipleResult(data)
                binding.vm!!.tongue_picture.clear()
                binding.vm!!.tongue_picture.addAll(selectList)
            }else if(requestCode == 100){
                binding.vm!!.evaluation_status.set(data!!.getIntExtra("evauationId",0))
            }
        }
    }
}